package com.kuang.springboot2board.controller;

import com.kuang.springboot2board.dao.DepartmentDao;
import com.kuang.springboot2board.dao.EmployeeDao;
import com.kuang.springboot2board.pojo.Department;
import com.kuang.springboot2board.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

/**
 * @author Gerald
 * @date 2022/6/14-15:28
 */

@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees=employeeDao.getAll();
        model.addAttribute("emps",employees);

        return "emp/list";
    }

    @RequestMapping("/addemployee")
    public String addemployee(Model model){
        Collection<Department> departments=departmentDao.getDepartments();
        model.addAttribute("depart",departments);
        return "addemployee";
    }

    @PostMapping("/toaddemployee")
    public String toaddemployee(Employee employee){
        employeeDao.save(employee);
        return "redirect:/emps";
    }

    @GetMapping("/updateEmployee")
    public String updateEmployee(Integer id,Model model){
        System.out.println(id);
        Employee employee=employeeDao.getEmployeeById(id);
        model.addAttribute("employee",employee);
        Collection<Department> departments=departmentDao.getDepartments();
        model.addAttribute("depart",departments);

        return "emp/update";
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
//        System.out.println(id);
        employeeDao.delete(id);
        return "redirect:/emps";
    }
}
